package com.zmc.product.infrastructure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zmc.product.infrastructure.enums.ProductPictureEnum;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmc.common.utils.PageUtils;
import com.zmc.common.utils.Query;

import com.zmc.product.infrastructure.dao.CommodityGoodsDetailPictureDao;
import com.zmc.product.infrastructure.entity.CommodityGoodsDetailPictureEntity;
import com.zmc.product.infrastructure.service.CommodityGoodsDetailPictureService;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author zmc
 */
@Service("commodityGoodsDetailPictureService")
public class CommodityGoodsDetailPictureServiceImpl extends ServiceImpl<CommodityGoodsDetailPictureDao, CommodityGoodsDetailPictureEntity> implements CommodityGoodsDetailPictureService {

    @Override
    public List<CommodityGoodsDetailPictureEntity> getPicturesBySpuId(String spuId) {
        LambdaQueryWrapper<CommodityGoodsDetailPictureEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommodityGoodsDetailPictureEntity::getSpuId, spuId);
        return list(wrapper);
    }

    @Override
    @Transactional
    public void savePictures(String picture1, String picture2, String spuId) {
        List<CommodityGoodsDetailPictureEntity> pictureList = new ArrayList<>();
        Arrays.stream(picture1.split(",")).forEach(picture -> {
            CommodityGoodsDetailPictureEntity pictureEntity = new CommodityGoodsDetailPictureEntity();
            pictureEntity.setSpuId(spuId);
            pictureEntity.setType(ProductPictureEnum.PRODUCT_DETAIL.getCode());
            pictureEntity.setPicturePath(picture);
            pictureList.add(pictureEntity);
        });
        Arrays.stream(picture2.split(",")).forEach(picture -> {
            CommodityGoodsDetailPictureEntity pictureEntity = new CommodityGoodsDetailPictureEntity();
            pictureEntity.setSpuId(spuId);
            pictureEntity.setType(ProductPictureEnum.PRODUCT_INTRODUCE.getCode());
            pictureEntity.setPicturePath(picture);
            pictureList.add(pictureEntity);
        });
        saveBatch(pictureList);
    }

    @Override
    @Transactional
    public void removeBySpuId(String spuId) {
        baseMapper.removeBySpuId(spuId);
    }

}